<template>
	<view class="goods-detail-container" v-if="goodsdetail.goods_name">
		<swiper autoplay="true" indicator-dots="true" interval="3000" duration="1000" circular="true">
			<swiper-item v-for="(item,i) in goodsdetail.pics" :key="i">
				<image :src="item.pics_big" @click="preview(i)"></image>
			</swiper-item>
		</swiper>
		<!-- 商品信息区域 -->
		<view class="box">
			<view class="price">￥{{goodsdetail.goods_price}}</view>
			<view class="body">
				<view class="name">{{goodsdetail.goods_name}}</view>
				<view class="favi">
					<uni-icons type="star" size="18" color="gray"></uni-icons>
					<text>收藏</text>
				</view>
				 <view class="yf">快递：免运费</view>
			</view>
		</view>
		<!-- 商品详情信息 -->
		<rich-text :nodes="goodsdetail.goods_introduce"></rich-text>
		<!-- 商品导航组件 -->
			<view class="nav">
					<uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
					@buttonClick="buttonClick"></uni-goods-nav>
			</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				goodsId: 0,
				goodsdetail: {},
				// 左侧按钮组的配置对象    
				options: [{
						icon: 'shop',
						text: '店铺',
					},
					{
						icon: 'cart',
						text: '购物车',
						info: 2,
					}
				],
				// 右侧按钮组的配置对象    
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: '#ff0000',
						color: '#fff',
					},
					{
						text: '立即购买',
						backgroundColor: '#ffa200',
						color: '#fff',
					}
				]
			};
		},
		onLoad(Option) {
			this.goodsId = Option.goods_id
			this.getGoodsDetail(this.goodsId)
		},
		methods: {
			async getGoodsDetail(id) {
				const goods = await uni.$http.get('goods_detail?goods_id=' + id)
				if (goods.statusCode !== 200) {
					return uni.$showMsg()
				}
				goods.data[0].goods_introduce = goods.data[0].goods_introduce.replace(/<img /g,
					'<img style="display:block;"').replace(/webp/g, 'jpg')
				this.goodsdetail = goods.data[0]
			},
			// 实现轮播图的预览效果
			preview(i) {
				// 调用 uni.previewImage() 方法预览图片
				uni.previewImage({
					current: i,
					urls: this.goodsdetail.pics.map(x => {
						x.pics_big
					})
				})
			},
			onClick(e){
				if(e.index==1){
					uni.switchTab({
						url:'/pages/cart/cart'
					})
				}
			},
			buttonClick(e){
				if(e.index ==0){
					this.options[1].info = this.options[1].info+1
				}
			}
		}
	}
</script>

<style lang="less">
	swiper {
		height: 750rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.box {
		padding: 10px;
		padding-right: 0;

		.price {
			color: #c00000;
			font-size: 18px;
			margin: 10px 0;
		}

		.body {
			display: flex;
			justify-content: space-between;

			.name {
				font-size: 13px;
				padding-right: 10px;
			}

			.favi {
				width: 120px;
				font-size: 12px;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				border-left: 1px solid #efefef;
				color: gray;
			}
		}

		.yf {
			margin: 10px 0;
			font-size: 12px;
			color: gray;
		}
	}
	.goods-detail-container {
		padding-bottom: 50px;
	}
	.nav {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
	}
</style>
